// <copyright file="SyndicationService.cs" company="Clever Software">
// Copyright (c) 2013, 2014 All Right Reserved, http://www.cleversoftware.by
//
// This source is subject to the Microsoft Permissive License.
// Please see the License.txt file for more information.
// All other rights reserved.
//
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY 
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
//
// </copyright>
// <author>Clever Software</author>
// <email>info@cleversoftware.by</email>
// <date>2014-02-04</date> 
// <summary>CleverCMS source code</summary>

namespace CleverCMS.Cms.Services
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.ServiceModel.Syndication;

    public class SyndicationService
    {
        //TODO: Do we need this syndication mechanism at the moment (Service)?
        //private readonly IPageRepository                    m_pageData;
        //private readonly ISettingRepository                 m_settingData;
        //private readonly IGenericRepository<BlogEntry>      m_blog;

        //public SyndicationService(IPageRepository                   pageData, 
        //                          ISettingRepository                settingData, 
        //                          IGenericRepository<BlogEntry>     blog)
        //{
        //    this.m_pageData = pageData;
        //    this.m_settingData = settingData;
        //    this.m_blog = blog;
        //}
        
        //public Sitemap CreateSitemap(Func<CmsPage, string> generateEntryUrl, string homePage)
        //{
        //    IEnumerable<CmsPage> entries = m_pageData.LoadAllPublished();
        //   // IEnumerable<BlogEntry> blog = m_blog.LoadAll();

        //    var defaultPageId = m_settingData.Get(Constant.Settings.DefaultPageId).Value;
        //    var sitemap = new Sitemap();
            
        //    foreach (CmsPage page in entries)
        //    {
        //        if (page.Alias == defaultPageId)
        //        {
        //            AddHomePage(sitemap, page, homePage);
        //            continue;
        //        }

        //        var url = new SitemapUrl();

        //        url.Location = generateEntryUrl(page);

        //        url.ChangeFrequency = CalculateFrequency(page.ModifiedAt);
        //        url.Priority = 0.7;
        //        url.LastModified = page.ModifiedAt.ToString();
        //        sitemap.Add(url);
        //    }


        //    return sitemap;
        //}

        //public SyndicationFeed BuildFeed(string websiteUrl, Func<CmsPage, string> generateEntryUrl)
        //{
        //    string rssFeedTitle = m_settingData.Get(Constant.Settings.SiteLogo).Value;
        //    string rssFeedDescr = m_settingData.Get(Constant.Settings.SiteSubLogo).Value;

        //    IEnumerable<CmsPage> entries = m_pageData.LoadLast(10);
        //    List<SyndicationItem> items =
        //        entries.Select(entry => new SyndicationItem(entry.SeoTitle,
        //                                                    SyndicationContent.
        //                                                        CreateHtmlContent(entry.PageContent),
        //                                                    new Uri(generateEntryUrl(entry)),
        //                                                    string.Format("item-id-{0}", entry.Id),
        //                                                    entry.CreatedAt)).ToList();

        //    return new SyndicationFeed(rssFeedTitle,
        //                               rssFeedDescr,
        //                               new Uri(websiteUrl),
        //                               "syndicationID",
        //                               DateTime.Now,
        //                               items);
        //}

        //private void AddHomePage(Sitemap sitemap, CmsPage page, string homePage)
        //{
        //    var url = new SitemapUrl();
        //    url.Location = homePage;
        //    url.ChangeFrequency = CalculateFrequency(page.ModifiedAt);
        //    url.Priority = 0.9;
        //    url.LastModified = page.ModifiedAt.ToString();
        //    sitemap.Insert(0,
        //                   url);
        //}

        //private bool IsDefaultPage(CmsPage page, int homePageId)
        //{
        //    return page.Id == homePageId;
        //}

        //private static ChangeFrequency CalculateFrequency(DateTime modifiedAt)
        //{
        //    ChangeFrequency frequency = ChangeFrequency.Hourly;
        //    if (modifiedAt < DateTime.Now.AddMonths(-12))
        //    {
        //        frequency = ChangeFrequency.Yearly;
        //    }
        //    else if (modifiedAt < DateTime.Now.AddDays(-60))
        //    {
        //        frequency = ChangeFrequency.Monthly;
        //    }
        //    else if (modifiedAt < DateTime.Now.AddDays(-14))
        //    {
        //        frequency = ChangeFrequency.Weekly;
        //    }
        //    else if (modifiedAt < DateTime.Now.AddDays(-2))
        //    {
        //        frequency = ChangeFrequency.Daily;
        //    }
        //    return frequency;
        //}
    }
}